//单向链表
public class MySingleList implements IList
{

    static class ListNone {
        public int val;
        public ListNone next;


        public ListNone(int val) {
            this.val = val;
        }

        public int getVal() {
            return val;
        }
    }

    public ListNone head;     //表示当前列表的头节点

    public int listSize;

    //手动创建一个链表
    public void createList() {
        ListNone node1 = new ListNone(12);
        ListNone node2 = new ListNone(23);
        ListNone node3 = new ListNone(34);
        ListNone node4 = new ListNone(45);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;

        this.head = node1;
    }

    @Override
    public void addFirst(int data) {
        ListNone node = new ListNone(data);
        node.next = head;
        head = node;
        listSize++;
//        if(head == null) {
//            head = node;
//        } else {
//            node.next = head;
//            head = node;
//        }
//
   }

    @Override
    public void addLast(int data) {
        ListNone node = new ListNone(data);
        if(head == null) {
            head = node;
            listSize++;
            return;
        }
        ListNone cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
        listSize++;
    }

    @Override
    public void addIndex(int index, int data) throws IndexOutOfBoundsException{

        checkIndex(index);

        ListNone node = new ListNone(data);

        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        ListNone cur = findIndex(index);

        node.next = cur.next;

        cur.next = node;

        listSize++;
    }

    private void checkIndex(int index) {
        if(index < 0 || index > size()) {
            throw new IndexOutOfBoundsException("插入index位置不合法！" + index);
        }
    }

    private ListNone findIndex(int index) {
        ListNone cur = head;
        int count = 0;
        while (count != index-1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }



    @Override
    public boolean contains(int key) {
        ListNone cur = head;
        while(cur != null) {
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {

        if(head == null) {
            return;
        }
        if(head.val == key) {
            head = head.next;
            listSize--;
            return ;
        }

        ListNone cur = findNode(key);
        if(cur == null){
            System.out.println("没有你要删除的数据");
            return;
        }
        ListNone del = cur.next;
        cur.next = del.next;
        listSize--;

    }
    //找到第一个遇到的key的前一个节点
    //没有找到，返回null
    private ListNone findNode(int key) {
        ListNone cur = head;
        while(cur.next != null) {
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    @Override
    public void removeAllKey(int key) {
            if(head == null) {
                return;
            }
//            while (head.val == key) {
//                head = head.next;
//            }             有可能出现空指针异常

            ListNone cur = head.next;
            ListNone prev = head;

            while(cur != null) {
                if(cur.val == key) {
                    prev.next = cur.next;
                    listSize--;
                } else {
                    prev = cur;
                }
            }
        if (head.val == key) {
            head = head.next;
            listSize--;
        }
    }

    @Override
    public int size() {
//        ListNone cur = head;
//        int useSize = 0;
//        while(cur != null) {
//            useSize++;
//            cur = cur.next;
//        }
//        return useSize;
        return listSize;
    }

    @Override
    public void clear() {
        head = null;
        listSize = 0;
    }

    @Override
    public void display() {
        ListNone cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }


    //从指定节点开始打印
    public void display(ListNone newHead) {
        ListNone cur = newHead;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }




    //链表反转
    public ListNone reverseList() {
        if(head == null) {
            return null;
        }
        //只有一个节点
        if(head.next == null) {
            return head;
        }

        ListNone cur = head.next;
        head.next = null;

        while(cur != null){
            ListNone curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }



    //链表的中间节点，方法一
    public ListNone middleNode() {
        if(head == null) {
            return head;
        }
        int len = size();
        int index = len / 2;
        ListNone cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //链表的中间节点，方法2
    public ListNone middleNode(ListNone head) {
        if(head == null) {
            return null;
        }
        ListNone fast = head;
        ListNone slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;    //走两步
            slow = slow.next;            //走一步
        }
        return slow;
    }

    //列表倒数第k个节点
    public int kthToLast(int k) {
        if(k <= 0 /*|| k > size()*/) {
            return -1;
        }
        ListNone fast = head;
        ListNone slow = head;

        for (int i = 0; i < k-1; i++) {
            fast = fast.next;
            if(fast == null) {
                return -1;
            }
        }
        while(fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow.val;



    }


}
